package com.teacher.game.state;

import android.util.Log;
import android.view.MotionEvent;
import com.teacher.game.framework.util.Painter;
import com.teacher.ellio.Assets;

public class  LoadState extends State {
    
    private float loadingTime = 0f;
    private boolean resourcesLoaded = false;
    private static final float MIN_LOADING_TIME = 1.0f; // 最少显示1秒
    
    @Override
    public void init() {
        Log.d("LoadState","开始加载资源");
        try {
            Assets.load();
            resourcesLoaded = true;
            Log.d("LoadState","资源加载完成");
        } catch (Exception e) {
            Log.e("LoadState", "资源加载失败", e);
            resourcesLoaded = true; // 即使失败也要继续
        }
    }

    @Override
    public void update(float delta) {
        loadingTime += delta;
        
        // 确保资源已加载且显示时间足够长
        if (resourcesLoaded && loadingTime >= MIN_LOADING_TIME) {
            // 播放启动音效（如果资源加载成功）
            try {
                Assets.playSound(Assets.startId);
            } catch (Exception e) {
                Log.w("LoadState", "播放启动音效失败", e);
            }
            setCurrState(new MenuState());
        }
    }

    @Override
    public void render(Painter g) {
        // 简单的加载界面
        g.setColor(0xFF87CEEB); // 天蓝色背景
        g.fillRect(0, 0, 800, 450);
        
        // 显示加载文字（如果painter支持）
        g.setColor(0xFFFFFFFF);
        // g.drawString("Loading...", 350, 225);
    }

    @Override
    public boolean onTouch(MotionEvent e, int scaleX, int scaleY) {
        return false;
    }
}
